﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleKit.FtpSourceCodeReading
{
    public class AddUserFtp
    {
        public Dictionary<string, UserDataItem> userNameDatabase = new Dictionary<string, UserDataItem>();
        string sFileName = @"c:\workspace\user.dat";

        public AddUserFtp()
        {
        }

        public void AddUserName(string userName, UserDataItem password)
        {
            userNameDatabase[userName] = password;
        }

        public void Save()
        {
            try
            {
                AddUserName("wudi", new UserDataItem() { Password = "123", StartDictionary="C:\\"});
                BinaryFormatter bf = new BinaryFormatter();
                FileStream fs = new FileStream(sFileName, FileMode.Create);
                bf.Serialize(fs, userNameDatabase);
                fs.Close();
            }
            catch (Exception ex)
            {
                
            }

        }

        public bool Load()
        {
            try
            {
                if (!System.IO.File.Exists(sFileName))
                {
                    return true;
                }
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                System.IO.FileStream fileStream = new System.IO.FileStream(sFileName, System.IO.FileMode.Open);
                userNameDatabase = formatter.Deserialize(fileStream) as Dictionary<string, UserDataItem>;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {

            }

            return true;
        }

        public Dictionary<string, UserDataItem> UserNameDatabase
        {
            get { return userNameDatabase; }
            set { userNameDatabase = value; }
        }

        public static void MyMain(string[] args)
        {
            AddUserFtp auf = new AddUserFtp();
            auf.Save();

            AddUserFtp auf1 = new AddUserFtp();
            auf1.Load();
            UserDataItem udi = auf1.UserNameDatabase["wudi"];

            Console.WriteLine(udi.Password + " " + udi.StartDictionary);
        }
    }
    [Serializable]
    public class UserDataItem
    {
        private string password="";
        private string startDictionary="C:\\";
        public UserDataItem()
        {
        }
        /// <summary>
        ///
        /// </summary>
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        /// <summary>
        ///
        /// </summary>
        public string StartDictionary
        {
            get { return startDictionary; }
            set { startDictionary = value; }
        }
    }

}
